import 'dart:convert';
import 'dart:io';

import 'package:ladder_cash/app/com/model/login_user_info_model.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'package:ladder_cash/app/com/http/http_request.dart';
import 'package:ladder_cash/app/com/plugin/common_plugin.dart';
import 'package:ladder_cash/app/page/home/home_page.dart';
import 'package:ladder_cash/gen/assets.gen.dart';
import 'package:ladder_cash/generated/assets.dart' as myAssets;
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:liuxlcommon/view/loading_view.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:ladder_cash/app/com/plugin/google_marker_plugin.dart';
import 'package:ladder_cash/app/com/service/reported_data_service.dart';
import 'package:ladder_cash/value/app_colors.dart';
import 'package:ladder_cash/value/app_const.dart';

import '../generated/l10n.dart';
import '../library/src/log_config.dart';
import '../library/src/log_manager.dart';
import '../library/src/printers/pretty_printer.dart';

class App {
  static late BuildContext appContext;
  static String? token;
  static String? area_code = "+53";
  static String? privacy = "https://himmat-limited.com/HIMPrivacy.html";
  static String localeCode = "en";

  static bool get localeEn => localeCode == "en";
  static LoginUserInfoModel userInfoModel = LoginUserInfoModel();

  static init() async {
    // if (Platform.isAndroid) {
    //   HttpRequest.setBaseUrl("http://147.139.3.184:8931/rupee");
    // }
    LogManager.init(LogConfig(
      printerParaMeter: PrettyPrinter(),
    ));
    eazyLoading();
    await SpUtils.init();
    getUserAndToken();
    LoadingView.textColor = AppColors.textSecondColor;
  }

  ///用户必须授权
  static init2() async {
    settingLoadViewEmptyView();
    if (Platform.isIOS) {
      CommonPlugin.initAdjust;
    } else {
      GoogleMarkerPlugin.init();
    }
    // Lconsole.init();
  }

  ///定义全局的 routeObserver 对象
  static final RouteObserver<PageRoute> routeObserver = RouteObserver();

  static getUserAndToken() {
    var json1 = SpUtils().getStorage("loginUserInfo");
    var token = SpUtils().getStorage("token");
    if (json1 != null && token != null) {
      App.token = token;
      userInfoModel = LoginUserInfoModel.fromJson(json.decode(json1));
    }
  }

  static bool isLogin() {
    return App.token != null;
  }



  static eazyLoading() {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 1500)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.custom
      ..maskType = EasyLoadingMaskType.custom
      ..indicatorSize = 44.0
      ..radius = 11.0
      ..backgroundColor = Colors.white
      ..indicatorColor = AppColors.primaryColor
      ..textColor = Colors.black
      ..progressColor = AppColors.primaryColor
      ..maskColor = Colors.black.withOpacity(0);
  }

  static logout() {
    ReportedDataService.reset();
    SpUtils().setStorage("token", null);
    SpUtils().setStorage("userInfo", null);
    App.token = null;
    // Get.delete<IndexLogic>();
    // Get.offAll(const LoginPage(), arguments: {'isBack': false},opaque: false);
    Get.offAll(HomePage());
  }

  static settingLoadViewEmptyView() {
    LoadingView.defaultEmptyView = Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(
              width:  179.w,
              height: 169.w,
              child: Image.asset(Assets.img.empty.path)),
          6.lsh,
          LText.easy(S.current.noMore, 14.sp, fw4,)
        ],
      ),
    );
    LoadingView.defaultErrorView = Center(
      child: LColumn(lStyle: LStyle(mainAxisSize: MainAxisSize.min), children: [
        // SizedBox(
        //   width: 209.w,
        //   child: Image.asset(Assets.icError),
        // ),
        LSizeBoxHeight(24),
        LText.easy(
            "Try Again", 14.sp, FontWeight.w600, AppColors.textSecondColor),
      ]),
    );
  }
}

